package KLTN;

import java.awt.Panel;



import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;

import de.bwaldvogel.liblinear.InvalidInputDataException;

import thesis.ThesisDDAGHackCode;
import thesis.ThesisHAH;

import libsvm.svm;

public class KLTN_UI {
	
	private JFrame MyFrame;
	private JProgressBar progressBar;
	private JProgressBar progressBar_DDAGSVM;
	private JProgressBar progressBar_HAH;
	private JFileChooser mFileChooser;
	
	private JTabbedPane tab;
	private Panel tab_SVC;
	private Panel tab_DDAGSVM;
	private Panel tab_HAH;
	
	private JTextField txt_train;
	private JTextField txt_train_DDAGSVM;
	private JTextField txt_train_HAH;
	
	private JButton btn_train;
	private JButton btn_model;
	private JButton btn_predict;
	
	private JButton btn_train_DDAGSVM;
	private JButton btn_model_DDAGSVM;
	private JButton btn_predict_DDAGSVM;
	
	private JButton btn_train_HAH;
	private JButton btn_model_HAH;
	private JButton btn_predict_HAH;
	
	private JTextArea txt_result;
	private JTextArea txt_result_DDAGSVM;
	private JTextArea txt_result_HAH;
	
	private String path_test, path_model, path_out, path_train;
	
	private int _time;
	
	private Task task;
	
	public KLTN_UI(){
		
		initUI();	
		build_UI(MyFrame);
		
	}
	
	//ham khoi tao
	private JFrame initUI(){
		
		MyFrame = new JFrame();
		mFileChooser = new JFileChooser();
		
		progressBar = new JProgressBar(0,100);
		progressBar.setValue(0);
		progressBar.setStringPainted(true);
		
		txt_result = new JTextArea();
		JScrollPane result_scroll = new JScrollPane (txt_result, 
				   JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		
		tab = new JTabbedPane();
		tab_SVC = new Panel();
		tab_DDAGSVM = new Panel();
		tab_HAH = new Panel();
		
		txt_train = new JTextField();		
		
		
		
		//su kien nut Train
		btn_train = new JButton("Train");
		btn_train.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				task = new Task();
				int returnval = mFileChooser.showOpenDialog(MyFrame);
				
				if(returnval == JFileChooser.APPROVE_OPTION){
					File selectFile = mFileChooser.getSelectedFile();
				    path_train = selectFile.getPath();
					try {
						
						svm_train train = new svm_train(path_train,1);						
						path_model = path_train + ".model";
						train.run(path_model);
						task.addPropertyChangeListener(new PropertyChangeListener() {
							
							@Override
							public void propertyChange(PropertyChangeEvent evt) {
								// TODO Auto-generated method stub
								if ("progress" == evt.getPropertyName()) {
						            int progress = (Integer) evt.getNewValue();
						            progressBar.setValue(progress);
						        } 
							}
						});
						task.execute();
						
						
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}					
				}
			}
			
		});
		
		//su kien nut model
		btn_model = new JButton("Model");
		btn_model.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				int returnval = mFileChooser.showOpenDialog(MyFrame);
				if(returnval == JFileChooser.APPROVE_OPTION){
					File selectFile = mFileChooser.getSelectedFile();
				    path_model = selectFile.getPath();
				}				
			}
		});
		
		//su kien nut predict
		btn_predict = new JButton("Predict");
		btn_predict.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int returnval = mFileChooser.showOpenDialog(MyFrame);
				if(returnval == JFileChooser.APPROVE_OPTION){
					File selectFile = mFileChooser.getSelectedFile();
				    path_test = selectFile.getPath();
				    path_out = path_test.replace(selectFile.getName(), "result.data");
				    svm_predict predict = new svm_predict();
				    try {
						predict.run(path_test, path_model, path_out);
						double rate = predict.rate;
						String str_rate = "Ti le chinh xac:" + rate +"\n";						
						LoadTextFile loadfile = new LoadTextFile();
						String content = str_rate + loadfile.readTextFile(path_out);
						txt_result.setText(content);
						
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
		});
		// layout for tab_SVC
		SpringLayout layout_tab_SVC = new SpringLayout();
		
		layout_tab_SVC.putConstraint(SpringLayout.NORTH, btn_train,
                5,
                SpringLayout.NORTH, tab_SVC);
		
		layout_tab_SVC.putConstraint(SpringLayout.WEST, btn_train,
                5,
                SpringLayout.WEST, tab_SVC);
		
		layout_tab_SVC.putConstraint(SpringLayout.EAST, btn_train,
                -5,
                SpringLayout.WEST, progressBar);
		
		layout_tab_SVC.putConstraint(SpringLayout.NORTH, progressBar,
                8,
                SpringLayout.NORTH, tab_SVC);
		
		layout_tab_SVC.putConstraint(SpringLayout.EAST, progressBar,
                -5,
                SpringLayout.EAST, tab_SVC);
		
		layout_tab_SVC.putConstraint(SpringLayout.WEST, progressBar,
                90,
                SpringLayout.WEST, tab_SVC);
		
		layout_tab_SVC.putConstraint(SpringLayout.NORTH, btn_model,
                38,
                SpringLayout.NORTH, tab_SVC);
		
		layout_tab_SVC.putConstraint(SpringLayout.WEST, btn_model,
                5,
                SpringLayout.WEST, tab_SVC);		
		
		layout_tab_SVC.putConstraint(SpringLayout.NORTH, btn_predict,
                70,
                SpringLayout.NORTH, tab_SVC);
		
		layout_tab_SVC.putConstraint(SpringLayout.WEST, btn_predict,
                5,
                SpringLayout.WEST, tab_SVC);
		
		layout_tab_SVC.putConstraint(SpringLayout.NORTH, result_scroll,
                38,
                SpringLayout.NORTH, tab_SVC);
		
		layout_tab_SVC.putConstraint(SpringLayout.WEST,result_scroll,
                90,
                SpringLayout.WEST, tab_SVC);
		
		layout_tab_SVC.putConstraint(SpringLayout.EAST,result_scroll,
                -5,
                SpringLayout.EAST, tab_SVC);
		
		layout_tab_SVC.putConstraint(SpringLayout.SOUTH,result_scroll,
                -5,
                SpringLayout.SOUTH, tab_SVC);
		
		layout_tab_SVC.putConstraint(SpringLayout.EAST, btn_predict,
                -5,
                SpringLayout.WEST, result_scroll);
		
		layout_tab_SVC.putConstraint(SpringLayout.EAST, btn_model,
                -5,
                SpringLayout.WEST, result_scroll);
				
		tab_SVC.setLayout(layout_tab_SVC);
		
		//them cac dieu khien vao
		tab_SVC.add(btn_train);
		tab_SVC.add(progressBar);
		tab_SVC.add(btn_model);
		tab_SVC.add(btn_predict);
		tab_SVC.add(result_scroll);
		
		//Giao Dien DDAGSVM
		progressBar_DDAGSVM = new JProgressBar(0,100);
		progressBar_DDAGSVM.setValue(0);
		progressBar_DDAGSVM.setStringPainted(true);
		txt_result_DDAGSVM = new JTextArea();		
		txt_train_DDAGSVM = new JTextField();				
		
		JScrollPane result_scroll_DDAGSVM = new JScrollPane (txt_result_DDAGSVM, 
				   JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		
		//xu kien cua nut train DDAG
		btn_train_DDAGSVM = new JButton("Input");
		btn_train_DDAGSVM.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				int returnval = mFileChooser.showOpenDialog(MyFrame);
				
				if(returnval == JFileChooser.APPROVE_OPTION){
					File selectFile = mFileChooser.getSelectedFile();
				    path_train = selectFile.getPath();
				    txt_train_DDAGSVM.setText(path_train);				
					
				}
			}
		});
		
		//xu kien cua nut predict DDAG
		btn_predict_DDAGSVM = new JButton("Predict");
		btn_predict_DDAGSVM.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				ThesisDDAGHackCode thesisDDAGHackCode = new ThesisDDAGHackCode();
	
				try {
					thesisDDAGHackCode.main(path_train);
				} catch (IOException | InvalidInputDataException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				txt_result_DDAGSVM.setText(thesisDDAGHackCode.result);
			}
		});
		
		// layout for tab_DDAG
		SpringLayout layout_tab_DDAGSVM = new SpringLayout();		
		
		layout_tab_DDAGSVM.putConstraint(SpringLayout.NORTH, btn_train_DDAGSVM,
                5,
                SpringLayout.NORTH, tab_DDAGSVM);
		
		layout_tab_DDAGSVM.putConstraint(SpringLayout.WEST, btn_train_DDAGSVM,
                5,
                SpringLayout.WEST, tab_DDAGSVM);
		
		layout_tab_DDAGSVM.putConstraint(SpringLayout.NORTH, txt_train_DDAGSVM,
                8,
                SpringLayout.NORTH, tab_DDAGSVM);
		
		layout_tab_DDAGSVM.putConstraint(SpringLayout.WEST, txt_train_DDAGSVM,
                90,
                SpringLayout.WEST, tab_DDAGSVM);
		layout_tab_DDAGSVM.putConstraint(SpringLayout.EAST, txt_train_DDAGSVM,
                -5,
                SpringLayout.EAST, tab_DDAGSVM);
		
		layout_tab_DDAGSVM.putConstraint(SpringLayout.EAST, btn_train_DDAGSVM,
                -5,
                SpringLayout.WEST, txt_train_DDAGSVM);
		
		layout_tab_DDAGSVM.putConstraint(SpringLayout.NORTH, progressBar_DDAGSVM,
                40,
                SpringLayout.NORTH, tab_DDAGSVM);
		
		layout_tab_DDAGSVM.putConstraint(SpringLayout.EAST, progressBar_DDAGSVM,
                -5,
                SpringLayout.EAST, tab_DDAGSVM);
		
		layout_tab_DDAGSVM.putConstraint(SpringLayout.WEST, progressBar_DDAGSVM,
                90,
                SpringLayout.WEST, tab_DDAGSVM);
		
		
		layout_tab_DDAGSVM.putConstraint(SpringLayout.NORTH, btn_predict_DDAGSVM,
                38,
                SpringLayout.NORTH, tab_DDAGSVM);
		
		layout_tab_DDAGSVM.putConstraint(SpringLayout.WEST, btn_predict_DDAGSVM,
                5,
                SpringLayout.WEST, tab_DDAGSVM);
		
		layout_tab_DDAGSVM.putConstraint(SpringLayout.NORTH, result_scroll_DDAGSVM,
                70,
                SpringLayout.NORTH, tab_DDAGSVM);
		
		layout_tab_DDAGSVM.putConstraint(SpringLayout.WEST,result_scroll_DDAGSVM,
                5,
                SpringLayout.WEST, tab_DDAGSVM);
		
		layout_tab_DDAGSVM.putConstraint(SpringLayout.EAST,result_scroll_DDAGSVM,
                -5,
                SpringLayout.EAST, tab_DDAGSVM);
		
		layout_tab_DDAGSVM.putConstraint(SpringLayout.SOUTH,result_scroll_DDAGSVM,
                -5,
                SpringLayout.SOUTH, tab_DDAGSVM);
		
		layout_tab_DDAGSVM.putConstraint(SpringLayout.EAST, btn_predict_DDAGSVM,
                -5,
                SpringLayout.WEST, progressBar_DDAGSVM);
		
		tab_DDAGSVM.setLayout(layout_tab_DDAGSVM);
		
		//them cac dieu khien vao tab_DDAGSVM
		tab_DDAGSVM.add(btn_train_DDAGSVM);
		tab_DDAGSVM.add(btn_predict_DDAGSVM);
		tab_DDAGSVM.add(result_scroll_DDAGSVM);
		tab_DDAGSVM.add(progressBar_DDAGSVM);
		tab_DDAGSVM.add(txt_train_DDAGSVM);
		
		//Giao Dien HAH
		progressBar_HAH = new JProgressBar(0,100);
		progressBar_HAH.setValue(0);
		progressBar_HAH.setStringPainted(true);
		txt_result_HAH = new JTextArea();
		txt_train_HAH = new JTextField();		
				
		JScrollPane result_scroll_HAH = new JScrollPane (txt_result_HAH, 
				   JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
				
		//xu kien cua nut train HAH
		btn_train_HAH = new JButton("Input");
		btn_train_HAH.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				int returnval = mFileChooser.showOpenDialog(MyFrame);
				
				if(returnval == JFileChooser.APPROVE_OPTION){
					File selectFile = mFileChooser.getSelectedFile();
				    path_train = selectFile.getPath();
				    txt_train_HAH.setText(path_train);				
					
				}
			}
		});
				
		//xu kien cua nut predict DDAG
		btn_predict_HAH = new JButton("Predict");
		btn_predict_HAH.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				ThesisHAH thesisHAH = new ThesisHAH();
				try {
					thesisHAH.main(path_train);
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				txt_result_HAH.setText(thesisHAH.result);
			}
		});
				
		// layout for tab_HAH
		SpringLayout layout_tab_HAH = new SpringLayout();		
		
		layout_tab_HAH.putConstraint(SpringLayout.NORTH, btn_train_HAH,
                5,
                SpringLayout.NORTH, tab_HAH);
		
		layout_tab_HAH.putConstraint(SpringLayout.WEST, btn_train_HAH,
                5,
                SpringLayout.WEST, tab_HAH);
		
		layout_tab_HAH.putConstraint(SpringLayout.NORTH, txt_train_HAH,
                8,
                SpringLayout.NORTH, tab_HAH);
		
		layout_tab_HAH.putConstraint(SpringLayout.WEST, txt_train_HAH,
                90,
                SpringLayout.WEST, tab_HAH);
		layout_tab_HAH.putConstraint(SpringLayout.EAST, txt_train_HAH,
                -5,
                SpringLayout.EAST, tab_HAH);
		
		layout_tab_HAH.putConstraint(SpringLayout.EAST, btn_train_HAH,
                -5,
                SpringLayout.WEST, txt_train_HAH);
		
		layout_tab_HAH.putConstraint(SpringLayout.NORTH, progressBar_HAH,
                40,
                SpringLayout.NORTH, tab_HAH);
		
		layout_tab_HAH.putConstraint(SpringLayout.EAST, progressBar_HAH,
                -5,
                SpringLayout.EAST, tab_HAH);
		
		layout_tab_HAH.putConstraint(SpringLayout.WEST, progressBar_HAH,
                90,
                SpringLayout.WEST, tab_HAH);
		
		
		layout_tab_HAH.putConstraint(SpringLayout.NORTH, btn_predict_HAH,
                38,
                SpringLayout.NORTH, tab_HAH);
		
		layout_tab_HAH.putConstraint(SpringLayout.WEST, btn_predict_HAH,
                5,
                SpringLayout.WEST, tab_HAH);
		
		layout_tab_HAH.putConstraint(SpringLayout.NORTH, result_scroll_HAH,
                70,
                SpringLayout.NORTH, tab_HAH);
		
		layout_tab_HAH.putConstraint(SpringLayout.WEST,result_scroll_HAH,
                5,
                SpringLayout.WEST, tab_HAH);
		
		layout_tab_HAH.putConstraint(SpringLayout.EAST,result_scroll_HAH,
                -5,
                SpringLayout.EAST, tab_HAH);
		
		layout_tab_HAH.putConstraint(SpringLayout.SOUTH,result_scroll_HAH,
                -5,
                SpringLayout.SOUTH, tab_HAH);
		
		layout_tab_HAH.putConstraint(SpringLayout.EAST, btn_predict_HAH,
                -5,
                SpringLayout.WEST, progressBar_HAH);
		
		tab_HAH.setLayout(layout_tab_HAH);
		
		//them cac dieu khien vao tab_DDAGSVM
		tab_HAH.add(btn_train_HAH);
		tab_HAH.add(btn_predict_HAH);
		tab_HAH.add(result_scroll_HAH);
		tab_HAH.add(progressBar_HAH);
		tab_HAH.add(txt_train_HAH);
		
		//add childtab to tab
		tab.add(tab_SVC,"SVC");
		tab.add(tab_DDAGSVM,"DDAG");
		tab.add(tab_HAH,"HAH");
		
		MyFrame.add(tab);
		return MyFrame;
	}
	
	//ham build frame
	private static void build_UI(JFrame frame){
   		
		frame.setTitle("HOANG ANH HUNG PHAN TAN TAI PHAN DA LOP VAN BAN VOI DDAG VA HAH");
		frame.setSize(700, 375);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SwingUtilities.invokeLater(new Runnable() {
			@Override
            public void run() {
          	     new KLTN_UI();
            }
        });
	}

}

class Task extends SwingWorker<Void, Void> {
	
	@Override
    public Void doInBackground() {
        
		Random random = new Random();
        int progress = 0;
        //Initialize progress property.
        setProgress(0);
        while (progress < 100) {
            //Sleep for up to one second.
            try {
                Thread.sleep(random.nextInt(1000));
            } catch (InterruptedException ignore) {}
            //Make random progress.
            progress += random.nextInt(10);
            setProgress(Math.min(progress, 100));
            
        }
        return null;
    }
	
	@Override
    public void done() {
       
    }
}
